package com.zsc.oj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.zsc.oj.common.BaseResponse;
import com.zsc.oj.common.ErrorCode;
import com.zsc.oj.common.ResultUtils;
import com.zsc.oj.exception.BusinessException;
import com.zsc.oj.model.dto.friend.FriendApplyRequest;
import com.zsc.oj.model.dto.friend.SendMessageRequest;
import com.zsc.oj.model.entity.Friends;
import com.zsc.oj.model.entity.Message;
import com.zsc.oj.model.entity.User;
import com.zsc.oj.model.vo.FriendsVO;
import com.zsc.oj.model.vo.MessageVO;
import com.zsc.oj.service.FriendsService;
import com.zsc.oj.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/friend")
public class FriendsController {
    @Resource
    private FriendsService friendsService;

    @Resource
    private UserService userService;

    /**
     * 申请添加好友
     * @return
     */
    @PostMapping("/apply")
    public BaseResponse<Boolean> ApplyAddUser(@RequestBody FriendApplyRequest friendApplyRequest, HttpServletRequest request){
        if(friendApplyRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean result = friendsService.applyAddUser(friendApplyRequest,loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 展示谁向我发来申请的列表
     * @param request
     * @return
     */
    @GetMapping("/show/apply")
    public BaseResponse<List<FriendsVO>> ShowFriendsApplyList(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        List<FriendsVO> list = friendsService.showFriendsApplyList(loginUser);
        return ResultUtils.success(list);
    }

    /**
     * 展示我发出的申请列表
     * @param request
     * @return
     */
    @GetMapping("/show/send")
    public BaseResponse<List<FriendsVO>> ShowMySendList(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        List<FriendsVO> list = friendsService.showMySendList(loginUser);
        return ResultUtils.success(list);
    }

    /**
     * 同意好友申请
     * @return
     */
    @PostMapping("/agree/{fromId}")
    public BaseResponse<Boolean> AgreeApply(@PathVariable Long fromId,HttpServletRequest request){
        if(fromId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean result = friendsService.agreeApply(fromId,loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 拒绝好友申请
     * @param fromId
     * @param request
     * @return
     */
    @PostMapping("/disagree/{fromId}")
    public BaseResponse<Boolean> DisagreeApply(@PathVariable Long fromId,HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        if(fromId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = friendsService.disagreeApply(fromId,loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 查看申请数量
     * @param request
     * @return
     */
    @GetMapping("/count")
    public BaseResponse<Long> CountApply(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        long userId = loginUser.getId();
        QueryWrapper<Friends> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiveId",userId);
        queryWrapper.eq("status",0);
        long count = friendsService.count(queryWrapper);
        return ResultUtils.success(count);
    }

    /**
     * 删除好友
     * @param id
     * @param request
     * @return
     */
    @PostMapping("/delete/{id}")
    public BaseResponse<Boolean> DeleteFriend(@PathVariable Long id,HttpServletRequest request){
        if(id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean result = friendsService.deleteFriend(id,loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 消息发送
     * @param sendMessageRequest
     * @return
     */
    @PostMapping("/send")
    public BaseResponse<Boolean> sendMessage(@RequestBody SendMessageRequest sendMessageRequest){
        if(sendMessageRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = friendsService.sendMessage(sendMessageRequest);
        return ResultUtils.success(result);
    }

    /**
     * 获取信息
     * @param request
     * @return
     */
    @GetMapping("/get/message")
    public BaseResponse<List<MessageVO>> getMyMessage(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        List<MessageVO> message = friendsService.getMyMessage(loginUser.getId());
        return ResultUtils.success(message);
    }
}
